<template>
    <el-upload
        v-model:file-list="fileList"
        class="upload-demo"
        :action="actionApi"
        :on-preview="handlePreview"
        :on-remove="handleRemove"
        :before-remove="beforeRemove"
        :on-exceed="handleExceed"
        :on-change="handleChange"
    >
        <el-button type="primary">点击上传文件</el-button>
        <!--        <template #tip>-->
        <!--            <div class="el-upload__tip">-->
        <!--                jpg/png files with a size less than 500KB.-->
        <!--            </div>-->
        <!--        </template>-->
    </el-upload>
</template>
<script lang="ts" setup>
import {ref, watch} from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

import type { UploadProps, UploadUserFile } from 'element-plus'
import {env, getAuthToken} from "@/support/helper";

const props = defineProps({
    action: {
        type: String,
        required: true,
        default: '/upload/file'
    },
    name: {
        type: String,
        default: 'file'
    },
    auto: {
        type: Boolean,
        default: true
    },
    // eslint-disable-next-line vue/no-reserved-props
    class: {
        type: String,
        default: 'w-24 h-24'
    },
    requestFrom: {
        type: String,
        default: 'Dashboard'
    },
    token: {
        type: String,
        required: true
    },
    ext: {
        type: Array,
        default: () => {
            return ['docx', 'pdf', 'txt', 'html', 'zip', 'tar', 'doc', 'css', 'csv', 'ppt', 'xlsx', 'xls', 'xml', 'avi', 'wmv', 'mpeg', 'mp4', 'mov', 'mkv', 'flv', 'f4v', 'm4v', 'rmvb']
        }
    },
    modelValue: {
        type: Array,
        default: () => [],
    },
})

const baseURL = env('VITE_BASE_URL')

const actionApi = ref<string>('')

actionApi.value = baseURL + props.action

const token = ref<string>()
token.value = 'Bearer ' + getAuthToken()

const fileList = ref<UploadUserFile[]>([])
const submitFiles = ref<UploadUserFile[]>([])

watch(()=>props.modelValue, (val)=>{
    console.log(val, '初始化文件列表的值')
    // 判断val是否为空，如果不为空则赋值给fileList
    if (val === null || val === undefined || val.length === 0) {
        return
    }
    fileList.value = val
})

const handleRemove: UploadProps['onRemove'] = (file, uploadFiles) => {
    // console.log(file, uploadFiles,'handleRemove')
    // console.log(fileList.value,'fileList.value-删除触发')
    submitFiles.value = [];
    uploadFiles.forEach((item, index) => {
        // console.log(item, 'item的response值handleRemove')
        // console.log(index, 'index的值handleRemove')
        submitFiles.value[index] = item
    })
    // console.log(submitFiles.value,'submitFiles的值handleRemove')
    emit('update:modelValue', submitFiles.value)
}

const handlePreview: UploadProps['onPreview'] = (uploadFile) => {
    console.log(uploadFile,'handlePreview')
}

const handleExceed: UploadProps['onExceed'] = (files, uploadFiles) => {
    // ElMessage.warning(
    //     `The limit is 3, you selected ${files.length} files this time, add up to ${
    //         files.length + uploadFiles.length
    //     } totally`
    // )
    ElMessage.warning('超出上传数量限制');
}

const beforeRemove: UploadProps['beforeRemove'] = (uploadFile, uploadFiles) => {
    return ElMessageBox.confirm(
        `确认删除 ${uploadFile.name} ?`
    ).then(
        () => true,
        () => false
    )
}
const emit = defineEmits(['update:modelValue'])

const handleChange: UploadProps['onChange'] = (uploadFile, uploadFiles) => {
    // console.log(uploadFile, 'uploadFile的值')
    // console.log(uploadFile.response,'uploadFile的response的值')
    // console.log(fileList.value,'fileList.value的值')
    fileList.value.forEach((item, index) => {
        // console.log(item.response, 'item的response值')
        // console.log(index, 'index的值')
        submitFiles.value[index] = item
    })
    // console.log(submitFiles.value,'submitFiles的值')
    emit('update:modelValue', submitFiles.value)
}
</script>
